<?php

namespace app\console\command;

use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;

class DownloadUploadImg extends Command
{
    protected function configure()
    {
        $this->setName('download_upload_img')
            ->setDescription('download video img');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->info("execute download video img");

        $configs = config('site');
        $upload = Config::get('upload');

        try {
            $videoList = Db::name('videolist')
//                ->where('status', '=', 'normal')
                ->select();
            foreach ($videoList as $video) {
                try {
                    var_dump($video['id']);
                    Log::info('begin to download video img:' . $video['img']);
                    var_dump(ROOT_PATH . 'public' . $video['img']);
//                    exit();
                    if (!file_exists(ROOT_PATH . 'public' . $video['img'])) {
                        $imgurl = $configs['imgurl'] . $video['img'];
                        var_dump($imgurl);

                        $savekey = $video['img'];
                        $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
                        $fileName = substr($savekey, strripos($savekey, '/') + 1);
                        var_dump($uploadDir, $fileName);

                        var_dump(is_dir(ROOT_PATH . 'public' . $uploadDir) || mkdir(ROOT_PATH . 'public' . $uploadDir, 0755, true));

                        $filename = ROOT_PATH . 'public' . $uploadDir . $fileName;
                        file_put_contents($filename, file_get_contents($imgurl));
//                        unlink($tempFile);
                    }
//                    exit();
                } catch (\Exception $exception) {
                    Log::error($exception->getMessage());
                    Log::error($exception->getTraceAsString());
                }
            }
        } catch (\Exception $exception) {
            Log::error($exception->getMessage());
            Log::error($exception->getTraceAsString());
//            $output->error($exception->getMessage());
        }
    }
}
